import { observable, action } from 'mobx';
import { Tools } from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';

export default class DetailParticipationStore {
  path;
  @observable participationList;
  @observable listLoading = true;

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  // 参与数量接口
  @action getCountDatas = (params) => {
    let options = {
      method: 'GET',
      url: `/api/cowork/participation/getCoworkParticipationCounts`,
      params
    };
    return Tools.callApi(options)
  }

  @action getListDatas = (params, path, currentTab) => {
    if (!this.participationList) {
      this.participationList = new ListViewPage.Store({ route: path || this.path });
    }
    try {
      let listUrl = '';
      switch(currentTab) {
        case 'parter':
          listUrl = 'getCoworkParterList';
          break;
        case 'unread':
          listUrl = 'getCoworkUnreadList';
          break;
        case 'quiter':
          listUrl = 'getCoworkQuiterList';
          break;
        default:
          break;
      }
      let options = {
        method: 'GET',
        url: `/api/cowork/participation/${listUrl}`,
        params
      };
      const listLoading = () => {this.listLoading = false}
      this.listLoading = true;
      // 清除列表分页数据
      this.participationList.clearData();
      Tools.callApi(options).then(
        action('fetchSuccess', (res) => {
          this.participationList.getData({ dataKey: res.sessionkey }, listLoading, true)
        })
      )
    } catch(e) {
      this.listLoading = false
    }
  }
}